from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from models import *

class InternalAddDummies(webapp.RequestHandler):
	def get(self):
		user = NarisaUser.add(1, 'iwat1', 'hr34WFyC')
		user2 = NarisaUser.add(2, 'iwat2', 'Xr34WFyC')
		user3 = NarisaUser.add(3, 'iwat3', 'XC34WFyC')

		Tag.findOrCreate('java', None)
		Tag.findOrCreate('cocoa', None)

		TagHistory.tagTopic('java', user, 100)

		TagHistory.tagTopic('java', user2, 100)
		TagHistory.tagTopic('cocoa', user2, 100)

		TagHistory.tagTopic('c', user3, 100)
		TagHistory.tagTopic('cocoa', user3, 100)
		TagHistory.tagTopic('java', user3, 100)

		self.redirect('/_ah/admin/')

class InternalResetTagHits(webapp.RequestHandler):
	def get(self):
		tags = Tag.all().fetch(1000)

		self.response.out.write('<pre>')

		for tag in tags:
			self.response.out.write('resetting tag:%s hits count to zero\n' % tag)

			tag.hits = 0
			tag.put()

		self.response.out.write('</pre>')

class InternalRefreshTagHits(webapp.RequestHandler):
	def get(self):
		self.response.out.write('<pre>')

		stats = TopicStat.all().fetch(1000)

		for stat in stats:
			self.response.out.write('counting hits for:%s\n' % stat)

			tagInStat = stat.tag

			tagInStat.hits += stat.hits
			tagInStat.put()

		self.response.out.write('</pre>')

class InternalCleanTags(webapp.RequestHandler):
	def get(self):
		self.response.out.write('<pre>')

		tags = Tag.all().filter('hits', 0).fetch(1000)

		for tag in tags:
			self.response.out.write('deleting zero hits tag:%s\n' % tag)
			tag.delete()

		self.response.out.write('</pre>')

class InternalInit(webapp.RequestHandler):
	def get(self):
		NarisaUser.add(1281, 'iwat', 'hr34WFyC')

		self.redirect('http://appengine.google.com/datastore/explorer?&app_id=narisamashup&version_id=1.331210505731360170')

application = webapp.WSGIApplication(
		[
				('/internal/addDummies', InternalAddDummies),
				('/internal/resetTagHits', InternalResetTagHits),
				('/internal/refreshTagHits', InternalRefreshTagHits),
				('/internal/cleanTags', InternalCleanTags),
				('/internal/init', InternalInit)
		],
		debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()

